package com.secsbrain.cloudconsole.remote.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.secsbrain.cloudconsole.remote.SsoRemoteService;
import com.secsbrain.common.ResponseResult;
import com.secsbrain.common.utils.DevOpsHttpUtils;
import com.secsbrain.sso.model.dto.system.UserDTO;
import com.secsbrain.sso.model.dto.system.UserDetailDTO;
import com.secsbrain.sso.model.dto.system.UserSimpleDTO;
import com.secsbrain.sso.service.facade.system.UserServiceFacade;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * SSO相关服务
 *
 * @author zhaoyi
 */
@Service
public class SsoRemoteServiceImpl implements SsoRemoteService {

    @Value("${sso.gateway:}")
    private String ssoGatewayUrl;

    @Value("${sso.env:}")
    private String ssoEnv;

    @Autowired
    private DevOpsHttpUtils httpUtils;

    @Autowired
    private UserServiceFacade userServiceFacade;

    @Override
    public ResponseResult<List<UserDetailDTO>> getUserDTOByRoleCode(String roleCode) {
        ResponseResult<List<UserDetailDTO>> result = null;
        if (StringUtils.isNotEmpty(ssoGatewayUrl) && StringUtils.isNotEmpty(ssoEnv)) {
            Map<String, Object> params = new HashMap<>();
            params.put("roleCode", roleCode);
            String jsonRes = httpUtils.getRequest(ssoGatewayUrl + "/user/getUserDTOByRoleId", ssoEnv, params);

            ResponseResult<JSONObject> jsonVo = JSON.parseObject(jsonRes, ResponseResult.class);
            result = new ResponseResult<>(null);
            BeanUtils.copyProperties(jsonVo, result);
            if (jsonVo.getData() != null) {
                List<UserDetailDTO> appList = JSON.parseArray(JSON.toJSONString(jsonVo.getData()), UserDetailDTO.class);
                result.setData(appList);
            }
            Map<String, Object> resultMap = JSON.parseObject(jsonRes, Map.class);
            //兼容 dc 协议
            //  {"code":0,"msg":"success","data":true,"success":true}
            if (resultMap.containsKey("success")) {
                boolean success = resultMap.containsKey("success");
                if (success) {
                    result.setCode(1);
                }
            }
        } else {
            result = userServiceFacade.getUserDTOByRoleCode(roleCode);
        }
        return result;
    }

    @Override
    public ResponseResult<UserDTO> getUserDTO(Long userId) {
        ResponseResult<UserDTO> result = null;
        if(StringUtils.isNotEmpty(ssoGatewayUrl)&&StringUtils.isNotEmpty(ssoEnv)){
            Map<String, Object> params=new HashMap<String, Object>();
            params.put("userId", userId);
            String jsonRes=httpUtils.getRequest(ssoGatewayUrl+"/user/getUser", ssoEnv, params);

            result=JSON.parseObject(jsonRes,ResponseResult.class);

            Map<String,Object> resultMap=JSON.parseObject(jsonRes,Map.class);
            //兼容 dc 协议
            //  {"code":0,"msg":"success","data":true,"success":true}
            if(resultMap.containsKey("success")){
                boolean success=resultMap.containsKey("success");
                if(success){
                    result.setCode(1);
                }
            }

        }else{
            result = userServiceFacade.getUserDTO(userId);
        }
        return result;
    }

    @Override
    public ResponseResult<List<UserDetailDTO>> getRoleDTOsByUserId(String appCode, Long[] userIds) {
        ResponseResult<List<UserDetailDTO>> result = null;
        if (StringUtils.isNotEmpty(ssoGatewayUrl) && StringUtils.isNotEmpty(ssoEnv)) {
            Map<String, Object> params = new HashMap<>();
            params.put("appCode", appCode);
            params.put("userIds", userIds);
            String jsonRes = httpUtils.getRequest(ssoGatewayUrl + "/user/userRoleDtos", ssoEnv, params);

            ResponseResult<JSONObject> jsonVo = JSON.parseObject(jsonRes, ResponseResult.class);
            result = new ResponseResult<>(null);
            BeanUtils.copyProperties(jsonVo, result);
            if (jsonVo.getData() != null) {
                List<UserDetailDTO> appList = JSON.parseArray(JSON.toJSONString(jsonVo.getData()), UserDetailDTO.class);
                result.setData(appList);
            }
            Map<String, Object> resultMap = JSON.parseObject(jsonRes, Map.class);
            //兼容 dc 协议
            //  {"code":0,"msg":"success","data":true,"success":true}
            if (resultMap.containsKey("success")) {
                boolean success = resultMap.containsKey("success");
                if (success) {
                    result.setCode(1);
                }
            }
        } else {
            result = userServiceFacade.getRoleDTOsByUserId(appCode, userIds);
        }
        return result;
    }

    @Override
    public ResponseResult<List<UserDetailDTO>> getUserDTOByIds(Long[] userIds) {
        ResponseResult<List<UserDetailDTO>> result = null;
        if (StringUtils.isNotEmpty(ssoGatewayUrl) && StringUtils.isNotEmpty(ssoEnv)) {
            Map<String, Object> params = new HashMap<>();
            params.put("userIds", userIds);
            String jsonRes = httpUtils.getRequest(ssoGatewayUrl + "/user/getUserDTOByIds", ssoEnv, params);

            ResponseResult<JSONObject> jsonVo = JSON.parseObject(jsonRes, ResponseResult.class);
            result = new ResponseResult<>(null);
            BeanUtils.copyProperties(jsonVo, result);
            if (jsonVo.getData() != null) {
                List<UserDetailDTO> appList = JSON.parseArray(JSON.toJSONString(jsonVo.getData()), UserDetailDTO.class);
                result.setData(appList);
            }
            Map<String, Object> resultMap = JSON.parseObject(jsonRes, Map.class);
            //兼容 dc 协议
            //  {"code":0,"msg":"success","data":true,"success":true}
            if (resultMap.containsKey("success")) {
                boolean success = resultMap.containsKey("success");
                if (success) {
                    result.setCode(1);
                }
            }
        } else {
            result = userServiceFacade.getUserDTOByIds(userIds);
        }
        return result;
    }

    @Override
    public ResponseResult<List<UserSimpleDTO>> getDevelopers() {
        ResponseResult<List<UserSimpleDTO>> result = null;
        if (StringUtils.isNotEmpty(ssoGatewayUrl) && StringUtils.isNotEmpty(ssoEnv)) {
            Map<String, Object> params = new HashMap<>();
            String jsonRes = httpUtils.getRequest(ssoGatewayUrl + "/user/getDevelopers", ssoEnv, params);

            ResponseResult<JSONObject> jsonVo = JSON.parseObject(jsonRes, ResponseResult.class);
            result = new ResponseResult<>(null);
            BeanUtils.copyProperties(jsonVo, result);
            if (jsonVo.getData() != null) {
                List<UserSimpleDTO> appList = JSON.parseArray(JSON.toJSONString(jsonVo.getData()), UserSimpleDTO.class);
                result.setData(appList);
            }
            Map<String, Object> resultMap = JSON.parseObject(jsonRes, Map.class);
            //兼容 dc 协议
            //  {"code":0,"msg":"success","data":true,"success":true}
            if (resultMap.containsKey("success")) {
                boolean success = resultMap.containsKey("success");
                if (success) {
                    result.setCode(1);
                }
            }
        } else {
            result = userServiceFacade.getDevelopers();
        }
        return result;    }
}
